package com.zj.leetcode.easy.tree;

/**
 * @program: algorithm
 * @description: 左叶子之和
 * 404 sum-of-left-leaves
 * @author: Zhang Bo
 * @create: 2022-06-25 11:24:53
 **/
public class SumOfLeftLeaves {
    public static void main(String[] args) {
        Solution solution = new SumOfLeftLeaves().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution {
        int leftSum = 0;

        public int sumOfLeftLeaves(TreeNode root) {
            if (root == null) {
                return 0;
            }
            this.dfs(root, false);
            return leftSum;
        }

        private void dfs(TreeNode node, boolean ifLeft) {
            if (ifLeft && node.left == null && node.right == null) {
                leftSum += node.val;
            }
            if (node.left != null) {
                this.dfs(node.left, true);
            }

            if (node.right != null) {
                this.dfs(node.right, false);
            }

        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
